﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;


namespace Webparshing
{
    public class HhmlParser
    {

        public HhmlParser()
        {
        }
        public String GetHtmlString(String url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
            String strHtml = reader.ReadToEnd();

            //strHtml = Regex.Replace(strHtml, @"<(.|\n)*?>", String.Empty);
            strHtml = Regex.Replace(strHtml, @"<(.|\n)*?>", String.Empty);
            strHtml = strHtml.Replace(" ", "").Replace("\t", "").Replace("//-->", "").Replace("&nbsp", " ");
            String[] str = strHtml.Split(new Char[] { '\n' });
            strHtml = null;
            foreach (String s in str)
            {
                if (s.Trim() != "")
                    strHtml += s + ":";
            }

            reader.Close();
            response.Close();

            return strHtml;
        }
        public String GetHtmlStringWihtBrChangePipe(String strHtml)
        {
            strHtml = Regex.Replace(strHtml, @"<BR>", "|");
            strHtml = Regex.Replace(strHtml, @"<br>", "|");
            strHtml = Regex.Replace(strHtml, @"<(.|\n)*?>", String.Empty);
            strHtml = strHtml.Replace(" ", "").Replace("\t", "").Replace("//-->", "").Replace("&nbsp", " ");
            String[] str = strHtml.Split(new Char[] { '\n' });
            strHtml = null;
            foreach (String s in str)
            {
                if (s.Trim() != "")
                    strHtml += s + ":";
            }
            return strHtml;
        }
        public string HttpWebRequewFormPost(String Url, String parameters)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Url);
            req.Method = "Post";
            string s = parameters;

            req.CookieContainer = new CookieContainer();
            req.ContentLength = s.Length;
            req.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
            TextWriter w = (TextWriter)new System.IO.StreamWriter(req.GetRequestStream());
            w.Write(s);
            w.Close();
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(resp.GetResponseStream(), Encoding.Default);
            String strHtml = reader.ReadToEnd();
            return strHtml;
        }


        public void HttpWebResponseFormPost(WebBrowser webBrowser ,String Url, String parameters)
        {
                string strPostData = parameters;
                byte[] postData = Encoding.Default.GetBytes(strPostData);
                webBrowser.Navigate(Url, null, postData, "Content-Type: application/x-www-form-urlencoded"); 
        }
    }
}
